# -*- coding: utf-8 -*-
import scrapy
import re

# from scrapy.utils.project import get_project_settings
from scrapy_ndd.utils.common import get_conf
from scrapy.http import Request, FormRequest
from urllib import parse
from scrapy.loader import ItemLoader
from scrapy_ndd.items import ScrapyNddItem
from scrapy_ndd.utils.common import get_video_id, get_video_obj, get_create_time


class PPXNewVideoSpider(scrapy.Spider):

    name = 'ppx_new_video'
    settings = get_conf()
    ppx_host = settings.get('PPX_HOST', '')
    ppx_user = settings.get('PPX_USER', '')
    ppx_password = settings.get('PPX_PASSWORD', '')
    ppx_sessid = settings.get('PHPSESSID', '')
    allowed_domains = [ppx_host.split('://')[-1].split('/')[0]]
    # start_urls = ['https://www.ppx32.com/']

    def start_requests(self):
        ppx_url = "{host}/latest-updates/".format(host=self.ppx_host)
        cookies = {
            'PHPSESSID': self.ppx_sessid
        }
        yield FormRequest(ppx_url,
                          cookies=cookies,
                          dont_filter=True,
                          callback=self.parse)

    def parse(self, response):
        """
        提取元素
        :param response:
        :return:
        """
        # 1.获取列表页所有url交给scrapy解析
        post_nodes = response.css('.margin-fix .item a')
        for post_node in post_nodes:
            post_url = post_node.css('a::attr(href)').extract_first("")
            yield Request(url=parse.urljoin(response.url, post_url),
                          headers=response.request.headers,
                          cookies=response.request.cookies,
                          dont_filter=True,  # 开启则不会过滤掉没有定义的地址
                          callback=self.parse_details)

        # 2.获取下一页
        next_url = response.css('.next a::attr(data-parameters)').extract_first("")
        next_page = next_url.split('from:')[-1]
        if next_page is not None and len(next_page) > 0:
            formdata = {
                'mode': 'async',
                'function': 'get_block',
                'block_id': 'list_videos_latest_videos_list',
                'sort_by': 'post_date',
                'from': next_page
            }
            yield FormRequest(url=response.url,
                              headers=response.request.headers,
                              cookies=response.request.cookies,
                              formdata=formdata,
                              dont_filter=True,
                              callback=self.parse)

    def parse_details(self, response):
        item_loader = ItemLoader(item=ScrapyNddItem(), response=response)

        item_loader.add_value("video_id", get_video_id(response.text))
        item_loader.add_css("category", ".block-details div:nth-child(2) div:nth-child(2) a::text")
        item_loader.add_css("type", ".block-details div:nth-child(2) div:nth-child(2) a::text")
        item_loader.add_css("title", ".headline h1::text")
        item_loader.add_css("screenshot_url_1", ".block-screenshots a:nth-child(1)::attr(href)")
        item_loader.add_css("screenshot_url_2", ".block-screenshots a:nth-child(2)::attr(href)")
        item_loader.add_css("screenshot_url_3", ".block-screenshots a:nth-child(3)::attr(href)")
        item_loader.add_css("screenshot_url_4", ".block-screenshots a:nth-child(4)::attr(href)")
        item_loader.add_css("screenshot_url_5", ".block-screenshots a:nth-child(5)::attr(href)")
        video_obj = get_video_obj(response.text)
        item_loader.add_value("videos_screenshots", [video_obj["video"]["pic"]])
        if len(video_obj.get("video", {}).get("quality", [])) > 1:
            item_loader.add_value("video_url", [video_obj.get("video", {}).get("quality", [])[0]["url"]])
            item_loader.add_value("video_alt_url", [video_obj.get("video", {}).get("quality", [])[1]["url"]])
        else:
            item_loader.add_value("video_url", [video_obj.get("video", {}).get("quality", [])[0]["url"]])
        item_loader.add_css("download_url", ".block-details div:nth-child(2) div:nth-child(4) a::attr(href)")
        item_loader.add_css("views", ".block-details div:nth-child(2) div:nth-child(1) span:nth-child(2) em::text")
        item_loader.add_value("md5", "")
        item_loader.add_css("size", ".block-details div:nth-child(2) div:nth-child(4) a::text")
        item_loader.add_css("duration", ".block-details div:nth-child(2) div:nth-child(1) span:nth-child(1) em::text")
        item_loader.add_value("status", "")
        item_loader.add_css("tag", ".block-details div:nth-child(2) div:nth-child(3) a::text")
        item_loader.add_value("vip", "")
        item_loader.add_value("create_time", get_create_time())

        ndd_item = item_loader.load_item()
        # 调用后传递到pipeline中去 记得setting里面打开设置
        yield ndd_item
